/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package boundary;

import entity.Event;
import entity.Participant;
import entity.ResearchEvent;
import entity.ResearchUser;
import entity.User;
import java.security.Principal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Vector;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import security.EventBean;


/**
 *
 * @author MONESI
 */

@Stateless
@Named
public class SearchEventManager {
    
    @PersistenceContext
    EntityManager em;
    
   
      
    @Inject
    Principal principal;
          
    List<User> utenti;
    
    @EJB
    UserFacade uf;
    
    
    public void save(ResearchEvent event) {
        
        int x;
        int n;
        List<Integer> i = em.createQuery("SELECT MAX(s.idTransazione) FROM SEARCHEVENT s").getResultList();
        
        x=i.size()-1;
        n=i.get(x);
        n++;
        event.setIdTransazione(n);
        
        em.persist(event);
        
    }
  
    
      public String name (){
       List <String> c = em.createQuery("SELECT e.name FROM EVENTS e JOIN SEARCHEVENT s WHERE s.idEvento = e.eventId").getResultList();                   
       return c.get(c.size()-1);
    }
    
    public String description (){
       List <String> c = em.createQuery("SELECT e.description FROM EVENTS e JOIN SEARCHEVENT s WHERE s.idEvento = e.eventId").getResultList();                   
       return c.get(c.size()-1);
    }
    
    public String creatore (){
       List <String> c = em.createQuery("SELECT e.creatormail FROM EVENTS e JOIN SEARCHEVENT s WHERE s.idEvento = e.eventId").getResultList();                   
       return c.get(c.size()-1);
    }
    
    public void delete (int id){
       Query q = em.createQuery("UPDATE EVENTS e SET e.cancella=1 WHERE e.eventId LIKE :id").setParameter("id", id);
       q.executeUpdate();
       
       
    }
    public Date startDate (){
       List <Date> c = em.createQuery("SELECT e.date FROM EVENTS e JOIN SEARCHEVENT s WHERE s.idEvento = e.eventId").getResultList();                   
       return c.get(c.size()-1);
    }
    
    public String place (){
       List <String> c = em.createQuery("SELECT e.place FROM EVENTS e JOIN SEARCHEVENT s WHERE s.idEvento = e.eventId").getResultList();                   
       return c.get(c.size()-1);
    }
   public Date endDate (){
       List <Date> c = em.createQuery("SELECT e.endDate FROM EVENTS e JOIN SEARCHEVENT s WHERE s.idEvento = e.eventId").getResultList();                   
       return c.get(c.size()-1);
    }
    
   
}
